package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.po.MemberTvSetRecord;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.member.sign.MemberSigninRecordBiz;
import com.hunan.api.catv.service.biz.system.SystemDictBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/10/29 0029
 * @package com.hunan.api.catv.mq.consumer
 * @company 湖南有线
 */
@Component
public class TvMemberSetConsumer {

    public static final Logger log = LoggerFactory.getLogger(TvMemberSetConsumer.class);
    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SystemDictBiz systemDictBiz;
    @Autowired
    MemberSigninRecordBiz memberSigninRecordBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.TV_MODIFY_SET_QUEUE, durable = "true"), exchange = @Exchange(value = MqConstants.TV_MODIFY_SET_EXCHANGE, durable = "true", type = "topic"), key = MqConstants.TV_MODIFY_SET_ROUTINGKEY))
    public void task(String msg) {
        log.info("用户修改大屏设置添加记录,{}:" + msg);
        try {
            JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
            // 1：开机启动模式  2：儿童模式限制时间
            Integer type = msgJson.get("type").getAsInt();
            Integer memberId = msgJson.get("memberId").getAsInt();
            String before = "";
            String after = "";
            String description = "";
            if (!ObjectUtils.isEmpty(msgJson.get("before"))) {
                before = msgJson.get("before").getAsString();
            }
            if (!ObjectUtils.isEmpty(msgJson.get("after"))) {
                after = msgJson.get("after").getAsString();
            }
            if (!ObjectUtils.isEmpty(msgJson.get("description"))) {
                description = msgJson.get("description").getAsString();
            }
            MemberTvSetRecord tvSetRecord = new MemberTvSetRecord();
            tvSetRecord.setModifyType(type);
            tvSetRecord.setMemberId(memberId);
            tvSetRecord.setModifyBefore(before);
            tvSetRecord.setModifyAfter(after);
            tvSetRecord.setCreateTime(new Date());
            tvSetRecord.setModifyDescription(description);
            tvSetRecord.insert();
        } catch (Exception e) {
            log.error("用户修改大屏设置添加失败：{}", e);
        }
    }

}
